package com.example.rocketmq.demos;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
@Slf4j
public class SyncProducer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("test-producer");
        producer.setNamesrvAddr("43.240.220.48:9876");
        try {
            producer.start();
            for (int i = 0; i < 10; i++) {
                String msg = "test message" + i;
                Message message = new Message("test-topic", "test-producer", msg.getBytes(StandardCharsets.UTF_8));
                SendResult result = producer.send(message);
                log.info("{} 已经发送了,返回结果是：{}",msg,result.toString());
            }
        } catch (MQClientException e) {
            throw new RuntimeException(e);
        }finally {
            producer.shutdown();
        }


    }
}
